package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.service.mongo.CommentService;
import com.itheima.utils.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;


/**
 * @author Guanxing
 * @date 2021/9/23
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存动态评论
     * commentType : 操作类型： 1-对动态点赞，2-对动态评论，3-对动态喜欢  4-对视频点赞   5-对视频评论    6-对评论点赞
     *
     * @param comment
     * @return
     */
    @Override
    public Integer saveMovementComment(Comment comment) {
        //根据动态id查询动态详情
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);

        //保存comment
        comment.setCreated(System.currentTimeMillis());
        comment.setPublishUserId(movement.getUserId());
        mongoTemplate.save(comment);

        //点赞数、评论数、喜欢数 + 1
        if (comment.getCommentType() == 1) {
            movement.setLikeCount(movement.getLikeCount() + 1);
        } else if (comment.getCommentType() == 2) {
            movement.setCommentCount(movement.getCommentCount() + 1);
        } else if (comment.getCommentType() == 3) {
            movement.setLoveCount(movement.getLoveCount() + 1);
        }

        //更新下寻新详情表
        mongoTemplate.save(movement);

        //返回次数
        if (comment.getCommentType() == 1) {
            return movement.getLikeCount();
        } else if (comment.getCommentType() == 2) {
            return movement.getCommentCount();
        } else if (comment.getCommentType() == 3) {
            return movement.getLoveCount();
        }

        return 0;
    }

    //删除动态评论
    @Override
    public Integer deleteMovementComment(Comment comment) {
        //根据id查询动态详情
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);

        //删除comment
        Query query = new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                        .and("userId").is(comment.getUserId())
                        .and("commentType").is(comment.getCommentType())
        );
        mongoTemplate.remove(query, Comment.class);

        //点赞数、评论数、喜欢数 - 1
        if (comment.getCommentType() == 1) {
            movement.setLikeCount(movement.getLikeCount() - 1);
        } else if (comment.getCommentType() == 2) {
            movement.setCommentCount(movement.getCommentCount() - 1);
        } else if (comment.getCommentType() == 3) {
            movement.setLoveCount(movement.getLoveCount() - 1);
        }

        //更新详情表
        mongoTemplate.save(movement);

        //返回次数
        if (comment.getCommentType() == 1) {
            return movement.getLikeCount();
        } else if (comment.getCommentType() == 2) {
            return movement.getCommentCount();
        } else if (comment.getCommentType() == 3) {
            return movement.getLoveCount();
        }
        return 0;
    }

    //查询评论列表
    @Override
    public PageBeanVo findCommentList(Integer pageNum, Integer pageSize, String movementId) {
        int startIndex = (pageNum - 1) * pageSize;
        //封装条件
        Query query = new Query(
                Criteria.where("publishId").is(new ObjectId(movementId))
                        .and("commentType").is(ConstantUtil.MOVEMENT_LIKE_2)
        ).with(Sort.by(Sort.Order.desc("created")))
                .skip(startIndex).limit(pageSize);

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        long count = mongoTemplate.count(query, Comment.class);

        return new PageBeanVo(pageNum, pageSize, count, commentList);
    }

    //展示点赞 评论 喜欢
    @Override
    public PageBeanVo findMessagesByCommentType(Integer pageNum, Integer pageSize, int commentType, Long loginUserId) {
        int startIndex = (pageNum - 1) * pageSize;
        //根据查询的类别查询相应表
        Query query = new Query(
                Criteria.where("commentType").is(commentType)
                        .and("publishUserId").is(loginUserId)
        );

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);

        return new PageBeanVo(pageNum, pageSize, count, commentList);
    }

    //对视频点赞
    @Override
    public void saveLike(Comment comment) {
        //根据视频id查询视频详情
        Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);

        //保存video
        comment.setPublishUserId(video.getUserId());
        mongoTemplate.save(comment);
    }

    //视频取消点赞
    @Override
    public void removeDislike(Comment comment) {
        Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);

        Query query = new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                        .and("userId").is(comment.getUserId())
                        .and("commentType").is(comment.getCommentType())
        );
        mongoTemplate.remove(query,Comment.class);

    }

    //保存评论
    @Override
    public void saveComment(Comment comment) {
        Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);

        comment.setPublishUserId(video.getUserId());
        mongoTemplate.save(comment);
    }

    //保存点赞
    @Override
    public void saveCommentLike(Comment comment) {
        Comment template = mongoTemplate.findById(comment.getPublishId(), Comment.class);

        comment.setPublishUserId(template.getUserId());
        mongoTemplate.save(comment);
    }

    //查询评论列表
    @Override
    public PageBeanVo findVideoCommentList(Integer pageNum, Integer pageSize, String videoId) {
        int startIndex = (pageNum - 1) * pageSize;
        Query query = new Query(
                Criteria.where("publishId").is(new ObjectId(videoId))
                        .and("commentType").is(ConstantUtil.MOVEMENT_LIKE_5)
        ).with(Sort.by(Sort.Order.desc("created")))
                .skip(startIndex).limit(pageSize);

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        return new PageBeanVo(pageNum, pageSize, count, commentList);
    }

    //取消评论点赞
    @Override
    public void removeComment(Comment comment) {
        Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);

        Query query = new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                        .and("commentType").is(ConstantUtil.MOVEMENT_LIKE_6)
                        .and("userId").is(comment.getUserId())
        );
        mongoTemplate.remove(query, Video.class);

    }

}
